package com.zhangck.bos2_0.action.system;


import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zhangck.bos2_0.Service.system.UserServices;
import com.zhangck.bos2_0.common.BaseAction;

import cn.itcast.bos.domain.system.User;

/** 
* @author 上午茶
* @version 创建时间：2018年6月16日 下午3:06:03 
* 类说明 
*/
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Actions
@Controller
public class UserLoginAction  extends BaseAction<User>{
	
	
	@Autowired
	private UserServices userService;

	@Action(value="userloing",results={@Result(name="success", type="redirect", location="/index.html"),
										@Result(name="erroe",type = "redirect",location="/login.html")})
	public String userloing(){
		Subject subject = SecurityUtils.getSubject();
		
		AuthenticationToken token = new UsernamePasswordToken(model.getUsername(), model.getPassword());
 
		try {
			subject.login(token);
			return "success";
		} catch (AuthenticationException e) {
			e.printStackTrace();
			return "erroe";
		}
		
	}
	
	@Action(value="user_logout",results={@Result(name="success", type="redirect", location="/login.html")})
	public String user_logout(){
		Subject subject = SecurityUtils.getSubject();
		
		subject.logout();
		
		return "success";
	}
	

	@Action(value = "user_list", results = { @Result(name = "success", type = "json") })
	public String list() {
		// 调用业务层 ，返回 用户列表
		List<User> users = userService.findAll();
		ActionContext.getContext().getValueStack().push(users);
		return SUCCESS;
	}
	
	// 属性驱动
	private String[] roleIds;

	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}

	@Action(value = "user_save", results = { @Result(name = "success", type = "redirect", location = "pages/system/userlist.html") })
	public String save() {
		// 调用业务层
		userService.saveUser(model, roleIds);
		return SUCCESS;
	}
	
}
